﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using XukaShop.Biz;
using XukaShop.Models.Entities;

namespace XukaShop.Areas.Admin.Controllers
{
    public class MemberController : Controller
    {
        private MemberBiz _memberBiz = new MemberBiz();

        [AllowAnonymous]
        public ActionResult Login()
        {
            var isLogged = HttpContext.User.Identity.IsAuthenticated;
            if (isLogged)
            {
                return RedirectToAction("Index", "Dashboard");
            }
            Member memberEnt = new Member();
            return View(memberEnt);
        }

        [HttpPost]
        [AllowAnonymous]
        public ActionResult Login(Member model)
        {
            if (ModelState.IsValid)
            {
                string passwordMD5 = _memberBiz.ConvertToMD5(model.Password);
                string userNameLower = model.UserName.ToLower();
                if (System.Web.Security.Membership.ValidateUser(userNameLower, passwordMD5))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, false);
                    Session["UserName"] = model.UserName;
                    return RedirectToAction("Index", "Dashboard");
                }
            }
            ViewBag.NotFoundUser = true;
            return View(model);
        }

        public ActionResult MemberList()
        {
            return View();
        }
    }
}
